package com.manthano.util.search;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DELL
 * @param <k>
 */
public class SearchCriteria<k> implements Serializable
{
	/**
	 * 
	 */
	private static final long			serialVersionUID			= 1673763077097865294L;
	/**
	 * MODE_RESULT
	 */
	public static final int				MODE_RESULT					= 0;
	/**
	 * MODE_TOTAL_COUNT
	 */
	public static final int				MODE_TOTAL_COUNT			= 1;
	/**
	 * MODE_RESULT_AND_TOTAL_COUNT
	 */
	public static final int				MODE_RESULT_AND_TOTAL_COUNT	= 2;
	private int							mode						= MODE_RESULT;

	private List<SearchCondition<k>>	conditions					= new ArrayList<SearchCondition<k>>();
	private Map<k, SearchCondition<k>>	mapConditions				= new HashMap<k, SearchCondition<k>>();

	private int							startIndex					= -1;
	private int							noOfRecords					= -1;
	private k							sortColumn					= null;
	private boolean						ascending					= false;
	private String						dataLevel					= null;

	/**
	 * 
	 */
	public SearchCriteria()
	{
	}

	/**
	 * @param mode
	 */
	public SearchCriteria(int mode)
	{
		this.mode = mode;
	}

	/**
	 * @return int
	 */
	public int getMode()
	{
		return mode;
	}

	/**
	 * @param mode
	 */
	public void setMode(int mode)
	{
		this.mode = mode;
	}

	/**
	 * @param column
	 * @param value
	 */
	public void addCondition(k column, Object value)
	{
		SearchCondition<k> searchCondition = new SearchCondition<k>();
		searchCondition.setColumn(column);
		searchCondition.setValue(value);
		conditions.add(searchCondition);
		mapConditions.put(column, searchCondition);
	}

	/**
	 * @param searchCondition
	 */
	public void addCondition(SearchCondition<k> searchCondition)
	{
		conditions.add(searchCondition);
		mapConditions.put(searchCondition.getColumn(), searchCondition);
	}

	/**
	 * @param searchConditions
	 */
	public void addConditions(List<SearchCondition<k>> searchConditions)
	{
		conditions.addAll(searchConditions);

		for (SearchCondition<k> searchCondition : searchConditions)
		{
			mapConditions.put(searchCondition.getColumn(), searchCondition);
		}
	}

	/**
	 * @param column
	 * @return searchConditions
	 */
	public SearchCondition<k> getCondition(k column)
	{
		return mapConditions.get(column);
	}

	/**
	 * @return List<SearchConditios
	 */
	public List<SearchCondition<k>> getConditions()
	{
		return conditions;
	}

	/**
	 * @return int
	 */
	public int getStartIndex()
	{
		return startIndex;
	}

	/**
	 * @param startIndex
	 */
	public void setStartIndex(int startIndex)
	{
		this.startIndex = startIndex;
	}

	/**
	 * @return mode
	 */
	public boolean reqTotalCount()
	{
		return (mode == SearchCriteria.MODE_TOTAL_COUNT) || (mode == SearchCriteria.MODE_RESULT_AND_TOTAL_COUNT);
	}

	/**
	 * @return mode
	 */
	public boolean reqResult()
	{
		return (mode == SearchCriteria.MODE_RESULT) || (mode == SearchCriteria.MODE_RESULT_AND_TOTAL_COUNT);
	}

	/**
	 * @return noOfRecords
	 */
	public int getNoOfRecords()
	{
		return noOfRecords;
	}

	/**
	 * @param noOfRecords
	 */
	public void setNoOfRecords(int noOfRecords)
	{
		this.noOfRecords = noOfRecords;
	}

	/**
	 * @return dataLevel
	 */
	public String getDataLevel()
	{
		return dataLevel;
	}

	/**
	 * @return the ascending
	 */
	public boolean isAscending()
	{
		return ascending;
	}

	/**
	 * @param ascending
	 *            the ascending to set
	 */
	public void setAscending(boolean ascending)
	{
		this.ascending = ascending;
	}

	/**
	 * @param dataLevel
	 */
	public void setDataLevel(String dataLevel)
	{
		this.dataLevel = dataLevel;
	}

	/**
	 * @return the sortColumn
	 */
	public k getSortColumn()
	{
		return sortColumn;
	}

	/**
	 * @param sortColumn
	 *            the sortColumn to set
	 */
	public void setSortColumn(k sortColumn)
	{
		this.sortColumn = sortColumn;
	}
}
